package com.wxmit.module.system.web;

import cn.hutool.core.lang.tree.Tree;
import com.wxmit.base.domain.ApiResult;
import com.wxmit.framework.satoken.core.util.StpUserUtil;
import com.wxmit.module.system.api.PermissionApiImpl;
import com.wxmit.module.system.domain.SysRole;
import com.wxmit.module.system.domain.SysUser;
import com.wxmit.module.system.domain.dto.UpdateUserPwDTO;
import com.wxmit.module.system.domain.vo.SysMenuRouteVO;
import com.wxmit.module.system.domain.vo.SysUserVO;
import com.wxmit.module.system.service.ISysMenuService;
import com.wxmit.module.system.service.ISysUserService;
import com.wxmit.system.domain.LoginUser;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  用户信息
 * @Author huangxiong
 * @Date 2022/8/5 09:26
 * @Version V1.0
 */
@RestController
@RequestMapping("")
@RequiredArgsConstructor
public class SysUserInfoController {

    private final PermissionApiImpl permissionApi;

    private final ISysMenuService sysMenuService;

    private  final ISysUserService userService;

    private final FileStorageService fileStorageService;


    @GetMapping("/userInfo")
    public SysUserVO getUserInfo(){
        SysUserVO sysUserInfo = userService.getUserInfo(StpUserUtil.getUserId());
        sysUserInfo.setPassword(null);
        List<String> permissionList = StpUserUtil.getPermissionList();
        sysUserInfo.setPermissionList(permissionList);
        return sysUserInfo;
    }

    @GetMapping("/routers")
    public SysMenuRouteVO getRouters(){
        SysUser user = StpUserUtil.getUser(SysUser.class);
        List<Tree<Long>> sysMenus;
        if(StpUserUtil.isAdmin()){
            sysMenus = sysMenuService.listTree();
        }else{
            List<Long> roleIds = user.getRoles().stream().map(SysRole::getId).collect(Collectors.toList());
            sysMenus = sysMenuService.listTree(roleIds);
        }
        return new SysMenuRouteVO().setRoutes(sysMenus).setHome(user.getRoles().get(0).getHomePage());
    }

    @PutMapping("/userInfo/avatar")
    public String updateAvatar(MultipartFile file) {
        FileInfo fileInfo = fileStorageService.of(file).setPath("avatar" + File.separator ).upload();
        userService.updateAvatar(StpUserUtil.getUserId(), fileInfo.getPath() + fileInfo.getFilename());
        SysUser user = StpUserUtil.getUser(SysUser.class);
        user.setAvatar(fileInfo.getUrl());
        LoginUser loginUser = StpUserUtil.getLoginUser();
        loginUser.setUserObj(user);
        StpUserUtil.setLoginUser(loginUser);
        return user.getAvatar();
    }

    @PutMapping("/userInfo")
    public ApiResult updateUserInfo(@RequestBody SysUser sysUser){
        sysUser.setId(StpUserUtil.getUserId());
        userService.updateById(sysUser);
        return ApiResult.success();
    }

    @PutMapping("/userInfo/pw")
    public ApiResult updateUserPw(@RequestBody @Validated UpdateUserPwDTO pw){
        userService.updatePw(pw);
        return ApiResult.success();
    }
}
